﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace System
{
    public static class Package
    {
        /// <summary>
        /// 获得object对象的name属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T GetPropertyValue<T>(this object obj, string name)
        {
            object value_obj = null;

            Type objType = obj.GetType();
            PropertyInfo[] propinfos = objType.GetProperties();
            foreach (var item in propinfos)
            {
                if (item.Name.Equals(name))
                {
                    value_obj = item.GetValue(obj, null);
                    break;
                }
            }
            if (value_obj == null)
            {
                return default(T);
            }
            else
            {
                try
                {

                    return (T)value_obj;
                }
                catch
                {
                    return default(T);
                }
            }
        }

        /// <summary>
        /// 设置Object对象的name属性值为value 返回是否成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetPropertyValue<T>(this object obj, string name, T value)
        {
            Type objType = obj.GetType();
            PropertyInfo[] propinfos = objType.GetProperties();

            PropertyInfo current = null;

            foreach (var item in propinfos)
            {
                if (item.Name.Equals(name))
                {
                    current = item;
                    break;
                }
            }
            if (current != null)
            {
                try
                {
                   ;
                   current.SetValue(obj, Convert.ChangeType(value, current.PropertyType), null);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// Object 执行方法 ref object rdata 返回值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="methodname"></param>
        /// <param name="parameters"></param>
        /// <param name="rdata"></param>
        /// <returns></returns>
        public static bool ExcuteMethod(this object obj, string methodname, object[] parameters, ref object rdata)
        {
            Type objType = obj.GetType();
            MethodInfo minfo = objType.GetMethod(methodname);
            if (minfo == null)
            {
                return false;
            }
            else
            {
                try
                {
                    DynamicMethodExecutor executor = new DynamicMethodExecutor(minfo);
                    object crdata = executor.Execute(obj, parameters);
                    rdata = crdata;
                    return true;
                }
                catch
                {
                    return false;
                }

            }

        }
        /// <summary>
        ///  Object 执行方法 无返回值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="methodname"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static bool ExcuteMethod(this object obj, string methodname, object[] parameters)
        {
            Type objType = obj.GetType();
            MethodInfo minfo = objType.GetMethod(methodname);
            if (minfo == null)
            {
                return false;
            }
            else
            {
                try
                {
                    DynamicMethodExecutor executor = new DynamicMethodExecutor(minfo);
                    executor.Execute(obj, parameters);
                    return true;
                }
                catch
                {
                    return false;
                }

            }
        }

        /// <summary>
        /// 判断对象是否实现某接口
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="interfacetype"></param>
        /// <returns></returns>
        public static bool ImplementInterface(this object obj, string interfacetype)
        {
            Type objType = obj.GetType();
            Type[] types = objType.GetInterfaces();

            foreach (Type item in types)
            {
                if (item.Name.ToUpper().Equals(interfacetype.ToUpper()))
                {
                    return true;
                }

            }

            return false;
        }



        /// <summary>
        /// 格式化字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string FormatMe(this string str,params object[] param)
        {
            return string.Format(str, param);
        }
    }
}
